{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf0542c5",
   "metadata": {},
   "source": [
    "# train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9f54e01",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "def train_test_split(*arrays,test_size=None,train_size=None,random_state=None,\n",
    "    shuffle=True,stratify=None,)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa066ea3",
   "metadata": {},
   "source": [
    "```\n",
    "*arrays: 单个数组或元组，表示需要划分的数据集。如果传入多个数组，则必须保证每个数组的第一维大小相同。\n",
    "test_size: 测试集的大小（占总数据集的比例，值为0.0-1.0，表示测试集占总样本比例）。默认值为0.25，即将传入数据的25%作为测试集。\n",
    "train_size: 训练集的大小（占总数据集的比例，值为0.0-1.0，表示训练集占总样本比例）。默认值为None，此时和test_size互补，即训练集的大小为(1-test_size)。\n",
    "random_state: 随机数种子。可以设置一个整数，用于复现结果。默认为None。其实是该组随机数的编号，在需要重复试验的时候，保证得到一组一样的随机数。（比如每次都填1，其他参数一样的情况下你得到的随机数组是一样的。但填0或不填，每次都会不一样。）\n",
    "shuffle: 是否随机打乱数据。默认为True。\n",
    "stratify: 可选参数，用于进行分层抽样。传入标签数组，保证划分后的训练集和测试集中各类别样本比例与原始数据集相同。默认为None，即普通的随机划分。（此参数作用是保持测试集与整个数据集里的数据分类比例一致，比如有1000个数据，800个属于A类，200个属于B类。设置stratify = y_lable，test_size=0.25，split之后数据组成如下：training: 750个数据，其中600个属于A类，150个属于B类；testing: 250个数据，其中200个属于A类，50个属于B类）\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13295a0f",
   "metadata": {},
   "source": [
    "# date_range"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "891febf9",
   "metadata": {},
   "source": [
    "```\n",
    "pandas.date_range 是 pandas 库中一个非常实用的函数，用于生成一个表示日期范围的索引，可用于创建时间序列数据。以下是一些常见的参数及其用法：\n",
    "start：表示日期范围的起始日期。可以是字符串或 datetime 对象。默认为 None。\n",
    "end：表示日期范围的结束日期。可以是字符串或 datetime 对象。默认为 None。\n",
    "periods：表示日期范围的时间数据的数量。默认为 None。\n",
    "freq：表示日期范围的频率。可以是字符串或 pandas 的 DatesFreq 类。默认为 ‘D’（每天）。\n",
    "tz：表示时区信息。可以是字符串或 pandas 的 TZInfo 类。默认为 None（无时区信息）。\n",
    "normalize：布尔值，表示是否将起始日期和结束日期规范化为午夜开始的时间。默认为 False。\n",
    "name：生成的日期范围的名称。默认为 None。\n",
    "closed：表示区间是左闭右开还是左开右闭。可以是 ‘left’ 或 ‘right’。默认为 ‘left’（左闭右开）。\n",
    "dtype：生成的日期范围的 dtype。默认为 ‘datetime64[ns]’。\n",
    "2.关于req参数，用于指定日期范围的频率，以下是freq参数的一些常用值：\n",
    "‘D’：表示每天。\n",
    "‘B’：表示每周（工作日之间，不包含周末）。\n",
    "‘W’：表示每周（包括周末）。\n",
    "‘M’：表示每月。\n",
    "‘Q’：表示每季度。\n",
    "‘A’：表示每年。\n",
    "此外，还可以使用相对日期偏移量，例如’3M’表示每三个月，'2Q’表示每两个季度。\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7ada2a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------1.freq=M按月生成，日期范围内的数据----------\n",
      "DatetimeIndex(['2023-01-31', '2023-02-28', '2023-03-31', '2023-04-30',\n",
      "               '2023-05-31', '2023-06-30', '2023-07-31', '2023-08-31',\n",
      "               '2023-09-30', '2023-10-31', '2023-11-30', '2023-12-31'],\n",
      "              dtype='datetime64[ns]', freq='M')\n",
      "--------2.periods指定日期范围内的时间数据数量---------\n",
      "DatetimeIndex(['2023-12-01 00:00:00', '2023-12-08 12:00:00',\n",
      "               '2023-12-16 00:00:00', '2023-12-23 12:00:00',\n",
      "               '2023-12-31 00:00:00'],\n",
      "              dtype='datetime64[ns]', freq=None)\n",
      "--------------3.时区为东八区的数据---------------\n",
      "DatetimeIndex(['2023-12-01 00:00:00+08:00', '2023-12-02 00:00:00+08:00',\n",
      "               '2023-12-03 00:00:00+08:00', '2023-12-04 00:00:00+08:00',\n",
      "               '2023-12-05 00:00:00+08:00'],\n",
      "              dtype='datetime64[ns, Asia/Shanghai]', freq='D')\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "print('1.freq=M按月生成，日期范围内的数据'.center(40, '-'))\n",
    "date_range = pd.date_range(start='2023-01-01', end='2023-12-31', freq='M')\n",
    "print(date_range)\n",
    "\n",
    "print('2.periods指定日期范围内的时间数据数量'.center(40, '-'))\n",
    "date_range = pd.date_range(start='2023-12-01', end='2023-12-31', periods=5)\n",
    "print(date_range)\n",
    "\n",
    "print('3.时区为东八区的数据'.center(40, '-'))\n",
    "date_range = pd.date_range(start='2023-12-01', end='2023-12-05', tz='Asia/Shanghai')\n",
    "print(date_range)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61b785f4",
   "metadata": {},
   "source": [
    "# sort_values\n",
    "```\n",
    "DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’, ignore_index=False, key=None)\n",
    "\n",
    "常用参数说明：\n",
    "by\t指定要进行排序的列名或索引值\n",
    "axis\t若 axis=0 或 ‘index’，则按照指定列的数据大小排序；若 axis=1 或 ‘columns’，则按照指定索引中数据大小排序。默认axis=0\n",
    "ascending\t若 ascending=True，则按照升序排序；若 ascending=False，则按降序排序，默认为True，即升序排序。如果这是一个 bool 列表，则必须匹配 by 的长度\n",
    "inplace\t排序后的数据是否替换原来的数据，默认为False，即不替换\n",
    "ignore_index\t是否重置索引，默认为不重置\n",
    "\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5fbc5a9",
   "metadata": {},
   "source": [
    "# pd.get_dummies"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db5b4f07",
   "metadata": {},
   "source": [
    "```\n",
    "data : array-like, Series, or DataFrame\n",
    "  输入数据\n",
    "prefix : string, list of strings, or dict of strings, default None\n",
    "  给输出的列添加前缀，如prefix=“A”,输出的列会显示类似\n",
    "\n",
    "prefix_sep：str, default ‘_’\n",
    "\n",
    "​   设置前缀跟分类的分隔符sepration，默认是下划线\"_\"\n",
    "\n",
    "dummy_na : bool, default False\n",
    "\n",
    "​   增加一列表示空缺值，如果False就忽略空缺值\n",
    "\n",
    "columns : list-like, default None\n",
    "\n",
    "​   指定需要实现类别转换的列名\n",
    "\n",
    "sparse：bool, default False\n",
    "\n",
    "  ​ 哑编码列是否应该支持SparseArray (True)或常规NumPy数组(False)。\n",
    "\n",
    "drop_first : bool, default False\n",
    "\n",
    "​   获得k中的k-1个类别值，去除第一个\n",
    "\n",
    "dtype：dtype, default np.uint8\n",
    "\n",
    "​   新列的数据类型，只允许一种类型的dtype\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a579418b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>302</td>\n",
       "      <td>Mike</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>504</td>\n",
       "      <td>Christine</td>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>708</td>\n",
       "      <td>Rob</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103</td>\n",
       "      <td>Daniel</td>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>303</td>\n",
       "      <td>Jennifer</td>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Id       Name     Sex\n",
       "0  302       Mike    Male\n",
       "1  504  Christine  Female\n",
       "2  708        Rob    Male\n",
       "3  103     Daniel    Male\n",
       "4  303   Jennifer  Female"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "students_df = pd.DataFrame({\n",
    "    'Id': [302, 504, 708, 103, 303],\n",
    "    'Name': [\"Mike\", \"Christine\", \"Rob\", \"Daniel\", \"Jennifer\"],\n",
    "    'Sex': ['Male', 'Female', 'Male', 'Male', 'Female'],\n",
    "})\n",
    "\n",
    "students_df_dummies = pd.get_dummies(students_df)\n",
    "students_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e205565b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Id</th>\n",
       "      <th>Name_Christine</th>\n",
       "      <th>Name_Daniel</th>\n",
       "      <th>Name_Jennifer</th>\n",
       "      <th>Name_Mike</th>\n",
       "      <th>Name_Rob</th>\n",
       "      <th>Sex_Female</th>\n",
       "      <th>Sex_Male</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>302</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>504</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>708</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>303</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Id  Name_Christine  Name_Daniel  Name_Jennifer  Name_Mike  Name_Rob  \\\n",
       "0  302               0            0              0          1         0   \n",
       "1  504               1            0              0          0         0   \n",
       "2  708               0            0              0          0         1   \n",
       "3  103               0            1              0          0         0   \n",
       "4  303               0            0              1          0         0   \n",
       "\n",
       "   Sex_Female  Sex_Male  \n",
       "0           0         1  \n",
       "1           1         0  \n",
       "2           0         1  \n",
       "3           0         1  \n",
       "4           1         0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students_df_dummies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7eafdcb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "287.594px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
